﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;
using FU.Capstones.IMS.Services.Common.Respondents;
using FU.Capstones.IMS.Services.Interfaces;

namespace FU.Capstones.IMS.Services.DAOs
{
    internal class MaterialLoafDAO : DAOBase, IMaterialLoafDAO
    {
        #region IMaterialLoafDAO Members

        public CollectionFeedback<MaterialLoaf> GetCollection(bool isShowAll)
        {
            //create a new collection feeback to hold retured value
            var collectionFeedback = new CollectionFeedback<MaterialLoaf>();
            try
            {
                var command = new SqlCommand("GetListMaterialLoaf", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                var dataTable = new DataTable("ListAllMaterialLoaf");
                var sqlDataAdapter = new SqlDataAdapter(command);
                sqlDataAdapter.Fill(dataTable);

                //create a list hold all material loaf
                var materialLoafList = new List<MaterialLoaf>();


                //check dataTable
                //if dataTable has any rows, add them into materialLoafList
                if (dataTable.Rows.Count != 0)
                {
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        var result = new MaterialLoaf
                                         {
                                             OrderId = Convert.ToString(dataRow["OrderID"]),
                                             MaterialName = Convert.ToString(dataRow["MaterialName"]),
                                             SupplierName = Convert.ToString(dataRow["ContactName"]),
                                             WarehouseName = Convert.ToString(dataRow["WarehouseName"]),
                                             ImportDate = Convert.ToDateTime(dataRow["Date"]),
                                             Quantity = Convert.ToInt32(dataRow["Quantity"]),
                                             PricePerUnit = (float) Convert.ToDouble(dataRow["PricePerUnit"])
                                         };
                        materialLoafList.Add(result);
                    }
                    collectionFeedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    collectionFeedback.IsSuccessful = true;
                    collectionFeedback.Data = materialLoafList;
                }
                else
                {
                    collectionFeedback.Message = InventoryConstants.Messages.GetFailed;
                    collectionFeedback.IsSuccessful = false;
                    collectionFeedback.Data = null;
                }
            }
            catch (SqlException sqlException)
            {
                collectionFeedback.Message = InventoryConstants.Messages.GetFailed;
                collectionFeedback.IsSuccessful = false;
                collectionFeedback.Data = null;
            }
            catch (Exception)
            {
                collectionFeedback.Message = InventoryConstants.Messages.GetFailed;
                collectionFeedback.IsSuccessful = false;
                collectionFeedback.Data = null;
            }
            finally
            {
                CloseConnection();
            }
            return collectionFeedback;
        }

        public CollectionFeedback<MaterialLoaf> GetCollectionType(bool isShowAll)
        {
            //create a new collection feeback to hold retured value
            var collectionFeedback = new CollectionFeedback<MaterialLoaf>();
            try
            {
                var command = new SqlCommand("GetListMaterialLoaf", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                var dataTable = new DataTable("ListAllMaterialLoaf");
                var sqlDataAdapter = new SqlDataAdapter(command);
                sqlDataAdapter.Fill(dataTable);

                //create a list hold all material loaf
                var materialLoafList = new List<MaterialLoaf>();


                //check dataTable
                //if dataTable has any rows, add them into materialLoafList
                if (dataTable.Rows.Count != 0)
                {
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        var result = new MaterialLoaf
                                         {
                                             OrderId = Convert.ToString(dataRow["OrderID"]),
                                             MaterialName = Convert.ToString(dataRow["MaterialName"]),
                                             SupplierName = Convert.ToString(dataRow["ContactName"]),
                                             WarehouseName = Convert.ToString(dataRow["WarehouseName"]),
                                             ImportDate = Convert.ToDateTime(dataRow["Date"]),
                                             Quantity = Convert.ToInt32(dataRow["Quantity"]),
                                             PricePerUnit = (float) Convert.ToDouble(dataRow["PricePerUnit"])
                                         };
                        materialLoafList.Add(result);
                    }
                    collectionFeedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    collectionFeedback.IsSuccessful = true;
                    collectionFeedback.Data = materialLoafList;
                }
                else
                {
                    collectionFeedback.Message = InventoryConstants.Messages.GetFailed;
                    collectionFeedback.IsSuccessful = false;
                    collectionFeedback.Data = null;
                }
            }
            catch (SqlException sqlException)
            {
                collectionFeedback.Message = InventoryConstants.Messages.GetFailed;
                collectionFeedback.IsSuccessful = false;
                collectionFeedback.Data = null;
            }
            catch (Exception)
            {
                collectionFeedback.Message = InventoryConstants.Messages.GetFailed;
                collectionFeedback.IsSuccessful = false;
                collectionFeedback.Data = null;
            }
            finally
            {
                CloseConnection();
            }
            return collectionFeedback;
        }

        public SingleFeedback<MaterialLoaf> GetSingle(int objectID)
        {
            throw new NotImplementedException();
        }

        public SingleFeedback<int> Insert(MaterialLoaf data)
        {
            throw new NotImplementedException();
        }

        public Feedback Update(MaterialLoaf data)
        {
            throw new NotImplementedException();
        }

        public Feedback Delete(int objID)
        {
            throw new NotImplementedException();
        }

        public CollectionFeedback<MaterialLoaf> Search(MaterialLoaf criteria)
        {
            throw new NotImplementedException();
        }

        public Feedback UndoDelete(int objID)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}